$(function () {
    inputprovince();
    cheakprovince();
    inputUsers();
})
//注入省份
function inputprovince() {
    $.ajax({
        type:"post",
        url:"/StoreManage/getProvince",
        data:{},
        dataType:"json",
        success:function(r){
            if (r!= null){
                var t="<option>请选择省份</option>";
                for (var i=0;i < r.length;i++){
                    t += "<option pid='"+r[i].pid+"'>"+r[i].pname+"</option>\n"
                }
                $(".forminfo>li:nth-child(3)>select:nth-of-type(1)").html(t);
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

/**
 * 根据省份确定市区
 */
function cheakprovince(){
    $(".forminfo>li:nth-child(3)>select:nth-of-type(1)").blur(function () {
        var r =$(".forminfo>li:nth-child(3)>select:nth-of-type(1)").val();
        if(r==="请选择省份"){
            var c="<option>请选择城市</option>";
            $(".forminfo>li:nth-child(3)>select:nth-of-type(2)").html(c);
        }else{
            $.ajax({
                type:"post",
                url:"/StoreManage/cheakprovince",
                data:{province:r},
                dataType:"json",
                success:function(r){
                    if (r!= null){
                        var q="<option>请选择城市</option>";
                        for (var i=0;i < r.length;i++){
                            q += "<option cid='"+r[i].cid+"'>"+r[i].cname+"</option>\n"
                        }
                        $(".forminfo>li:nth-child(3)>select:nth-of-type(2)").html(q);
                    }},
                error:function (r) {
                    alert("!!!")
                }
            })
        }
    })

}

/**
 * 注入仓管职位人员
 */
function inputUsers() {
    $.ajax({
        type:"post",
        url:"/StoreManage/getUserList",
        data:{},
        dataType:"json",
        success:function(r){
            if (r!= null){
                var q="          <option>请选择</option>\n";
                for (var i=0;i < r.length;i++){
                    q += "<option userid='"+r[i].userid+"'>"+r[i].name+"</option>\n"
                }
                $(".forminfo>li:nth-child(4)>select").html(q);
            }},
        error:function (r) {
            alert("!!!")
        }
    })
}

/**
 * 添加仓库
 */
function addStorage() {
    var zhenzeflag =/^1[3-9]{1}[0-9]{9}$/;
    var flag="true";
    var updatestoragename=$(".forminfo>li:nth-child(1)>input").val();
    var updateaddress = $(".forminfo>li:nth-child(2)>input").val();
    var udpateprovince=$(".forminfo>li:nth-child(3)>select:nth-of-type(1)").val();
    var updatecity=$(".forminfo>li:nth-child(3)>select:nth-of-type(2)").val();
    var updatefuzeman=$(".forminfo>li:nth-child(4)>select:nth-of-type(1)").val();
    var udpatetel=$(".forminfo>li:nth-child(5)>input").val();
    var updatedetail=$(".forminfo>li:nth-child(6)>textarea").val();
    var udpatestatusid=$(".forminfo>li:nth-child(7)>select:nth-of-type(1)").attr("value");
    var createman=$(".forminfo>li:nth-child(8)>input").val();
    var createtime=$(".forminfo>li:nth-child(9)>input").val();
    if(updatestoragename.length>30){
        alert("仓库名称不能大于30个字符！");
        flag="false";
    }else if(updateaddress.length>300){
        alert("仓库地址不能大于300个字符！");
        flag="false";
    }else if(udpatetel.length>300){
        alert("联系方式不能大于30个字符！");
        flag="false";
    }else if(udpateprovince=="请选择省份"){
        alert("仓库属地错误，请重新确认！");
        flag="false";
    }else if(updatecity=="请选择城市"){
        alert("仓库属地错误，请重新确认！");
        flag="false";
    }else if(updatefuzeman=="请选择"){
        alert("请确认负责人!");
        flag="false";
    }else if(!zhenzeflag.test(udpatetel)){
        alert("联系方式格式错误，请重新确认！");
        flag="false";
    }else if(updatestoragename==""||updateaddress==""||udpatetel==""){
        alert("有还未填的必填项，请重新确认！");
        flag="false";
    }

    if(flag=="true"){
        $.ajax({
            type:"post",
            url:"/StoreManage/addStorage",
            data:{updatestoragename:updatestoragename,updateaddress:updateaddress,udpateprovince:udpateprovince,updatecity:updatecity,updatefuzeman:updatefuzeman,udpatetel:udpatetel,updatedetail:updatedetail,udpatestatusid:udpatestatusid,createman:createman,createtime:createtime},
            dataType:"json",
            success:function(r){
                if (r!= null){
                    alert("仓库创建成功！即将返回仓库管理界面。。");
                    location.href="StorageList.html";
                }},
            error:function (r) {
                alert("!!!")
            }
        })
    }

}